/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bc;

import edu.upn.sigemfi.ope.bc.MonedaFacadeLocal;
import edu.upn.sigemfi.ope.be.Moneda;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 *
 * @author infzt
 */
public class MonedaDelegate {

    private MonedaFacadeLocal local;

    public MonedaDelegate(MonedaFacadeLocal local) {
        this.local = local;
    }

    public void crear(Moneda moneda) throws Exception {
        try {
            local.create(moneda);
        } catch (Exception ex) {
            local = lookupMonedaFacade();
            throw new Exception("Crear Moneda... " + ex.getMessage());
        }
    }

    public void editar(Moneda moneda) throws Exception {
        try {
            local.edit(moneda);
        } catch (Exception ex) {
            local = lookupMonedaFacade();
            throw new Exception("Editar Moneda... " + ex.getMessage());
        }
    }

    public void eliminar(Moneda moneda) throws Exception {
        try {
            local.remove(moneda);
        } catch (Exception ex) {
            local = lookupMonedaFacade();
            throw new Exception("Eliminar Moneda... " + ex.getMessage());
        }
    }

    public List<Moneda> getMonedas() throws Exception {
        List<Moneda> monedas;
        try {
            monedas = local.findAll();
        } catch (Exception ex) {
            local = lookupMonedaFacade();
            throw new Exception("Listar monedas... " + ex.getMessage());
        }
        return monedas;
    }

    private MonedaFacadeLocal lookupMonedaFacade() {
        try {
            Context c = new InitialContext();
            return (MonedaFacadeLocal) c.lookup("java:comp/env/MonedaFacade");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }
}
